package com.mofang.powerdekorhelper.utils;

import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/24 0024.
 */

public class SharePerenter {

    RetrofitSerives retrofitSerives;
    Call<ResultMessage> call;
    ISharedCallBack callBack;

    public void getSharePoint(int sellerId, Integer typeIndex, String typeName, Integer point, String title, Integer activityId, final ISharedCallBack callBack) {

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("seller_id", sellerId);
            jsonParams.put("type_index", typeIndex);
            jsonParams.put("type_name", typeName);
            jsonParams.put("point", point);
            jsonParams.put("title", title);
            if (activityId != null) {
                jsonParams.put("activity_id", activityId);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams.toString());

        call = retrofitSerives.getAddIntegral(body);

        call.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {

                if (response.isSuccessful()) {
                    callBack.onSuccess(response.body());
                } else {
                    callBack.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {

            }
        });
    }

    public interface ISharedCallBack {

        void onSuccess(ResultMessage resultMessage2);

        void onError(String errorInfo);
    }
}
